/******************************************************************************
 * @File name   :      ParameterServletRequestWrapper.java
 *
 * @Author      :      Wu Jianxi
 *
 * @Date        :      2011-9-26
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 DMS, Inc. All  Rights Reserved.
 * This software is published under the terms of the DMS Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2011-9-26 下午01:19:10        Wu Jianxi     1.0            Initial Version
 * 2011-10-14 下午3:59:09        Wu Jianxi     1.1            fix bug
 *****************************************************************************/
package com.cap.bts.framework.common.servlet;

import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 *
 */
public class ParameterServletRequestWrapper extends HttpServletRequestWrapper{

    private Map paraMap;
    
    
    public ParameterServletRequestWrapper(HttpServletRequest request, Map paraMap){
        super(request);
        this.paraMap = paraMap;
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2011-9-26
     * @see javax.servlet.ServletRequestWrapper#getParameterMap()
    **/
    @Override
    public Map getParameterMap() {
        return this.paraMap;
    }
    
    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2011-9-26
     * @see javax.servlet.ServletRequestWrapper#getParameterNames()
    **/
    @Override
    public Enumeration<String> getParameterNames() {
        if(null == this.paraMap ){
            return null;
        }
        return Collections.enumeration(paraMap.keySet());
    }
    
    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2011-9-26
     * @see javax.servlet.ServletRequestWrapper#getParameterValues(java.lang.String)
    **/
    @Override
    public String[] getParameterValues(String name) {
        if(null == this.paraMap){
            return null;
        }
        Object paramObjVal = paraMap.get(name);
        if(null == paramObjVal){
            return null;
        }else if(paramObjVal instanceof String[]){
            return (String[])paramObjVal;
        }else if(paramObjVal instanceof String){
            return new String[]{(String)paramObjVal};
        }else{
            return new String[]{String.valueOf(paramObjVal)};
        }
    }
    
    
    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2011-9-26
     * @see javax.servlet.ServletRequestWrapper#getParameter(java.lang.String)
    **/
    @Override
    public String getParameter(String name) {
        if(null == this.paraMap){
            return null;
        }
        
        Object paramObjVal = paraMap.get(name);
        if(null == paramObjVal){
            return null;
        }else if(paramObjVal instanceof String[]){
            String[] paramObjVals = (String[])paramObjVal;
            if(paramObjVals.length > 0){
                return paramObjVals[0];
            }else{
                return null;
            }
        }else if(paramObjVal instanceof String){
            return (String)paramObjVal;
        }else{
            return String.valueOf(paramObjVal);
        }
    }
    
}
